package com.sinosoft.dsp.platform.components.portal.input.elements;

import java.util.Date;

import org.hibernate.Query;

import com.sinosoft.builder.templates.concept.TagElementParameterType;
import com.sinosoft.builder.templates.concept.TagElementType;
import com.sinosoft.dsp.platform.components.helper.TagElementHelper;
import com.sinosoft.dsp.platform.exception.SinoPlatformException;
import com.sinosoft.dsp.system.core.log.Log;
import com.sinosoft.dsp.system.utils.Const;
import com.sinosoft.dsp.system.utils.DateUtils;

public class RangerTagElememt implements ISinoTagElement {

	private static final Log log = Log.getLog(RangerTagElememt.class.getName());

	public static final String PARAMETER_PATTERN = "pattern";

	public static final String PARAMETER_VALUE = "value";

	public static final String PARAMETER_RANGE = "range";

	public static final String PARAMETER_MIN = "min";

	public static final String PARAMETER_MAX = "max";

	public static final String PARAMETER_STEP = "step";

	private Date value;

	private int range;

	private int min = 0;

	private int max = 100;

	private int step = 1;

	@Override
	public boolean isEmpty() {
		return value == null;
	}

	@Override
	public void setParameter(Query query, String key) {
		if (!isEmpty()) {
			Date[] date = DateUtils.getRangeDate(value, range);
			query.setParameter(key + "_" + PARAMETER_MIN, date[0]);
			query.setParameter(key + "_" + PARAMETER_MAX, date[1]);
		}
	}

	public Date getValue() {
		return value;
	}

	public void setValue(Date value) {
		this.value = value;
	}

	public int getRange() {
		return range;
	}

	public void setRange(int range) {
		this.range = range;
	}

	public int getMin() {
		return min;
	}

	public void setMix(int min) {
		this.min = min;
	}

	public int getMax() {
		return max;
	}

	public void setMax(int max) {
		this.max = max;
	}

	public int getStep() {
		return step;
	}

	public void setStep(int step) {
		this.step = step;
	}

	@Override
	public void initValue(TagElementType type) throws SinoPlatformException {
		try {
			if (type != null) {
				TagElementParameterType patternParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_PATTERN);
				TagElementParameterType valueParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_VALUE);
				TagElementParameterType rangeParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_RANGE);
				TagElementParameterType minParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MIN);
				TagElementParameterType maxParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_MAX);
				TagElementParameterType stepParam = TagElementHelper
						.getTagElementParameter(type, PARAMETER_STEP);

				if (!TagElementHelper.isEmptyTagElementParameter(rangeParam)) {
					this.range = Integer.valueOf(rangeParam.getValue());
				}

				if (!TagElementHelper.isEmptyTagElementParameter(minParam)) {
					this.min = Integer.valueOf(minParam.getValue());
				}

				if (!TagElementHelper.isEmptyTagElementParameter(maxParam)) {
					this.max = Integer.valueOf(maxParam.getValue());
				}

				if (!TagElementHelper.isEmptyTagElementParameter(stepParam)) {
					this.step = Integer.valueOf(stepParam.getValue());
				}

				if (!Const.isEmpty(type.getValue())) {
					if (!TagElementHelper
							.isEmptyTagElementParameter(patternParam)) {
						this.value = DateUtils.str2dat(valueParam.getValue(),
								patternParam.getValue());
					} else {
						this.value = DateUtils.str2dat(valueParam.getValue());
					}

				}
			}
		} catch (Exception e) {
			log.error("初始化范围值出现错误。");
			throw new SinoPlatformException("初始化范围值出现错误。");
		}

	}
}
